﻿/******************************************************************
 *Company: http://www.xiaomutech.com/
 *fileName : qcardarea.h --- QCardArea
 *Auth       : yhni (QQ:393320854)
 *Create    : 2022/1/24
 *Description   :
 *Histroy:
 *<Auth>    <Date>        <Ver>        <Content>
 *         2022/1/24
 *******************************************************************/
#ifndef QCARDAREA_H
#define QCARDAREA_H

#include <QWidget>
#include <QMap>

#include "card_header.h"
#include "qcardhand.h"

class QCard;
typedef QList<QCard *> CardList;

inline bool operator == (const CardType& left, const CardType& right)
{
    return (left.point == right.point && left.suit == right.suit);
}

inline bool operator < (const CardType& left, const CardType& right)
{
    if (left.point == right.point) {
        return left.suit < right.suit;
    }

    return left.point < right.point;
}

inline bool operator > (const CardType& left, const CardType& right)
{
    if (left.point == right.point) {
        return left.suit > right.suit;
    }

    return left.point > right.point;
}

class QCard;
class QCardArea : public QWidget
{
    Q_OBJECT
public:
    explicit QCardArea(QWidget *parent = nullptr);
    ~QCardArea();

    void setAreaType(const AreaType &_areaType);
    void initCards();

    void addCard(QCard *_card);
    void addCards(CardList _cards);
    CardList getCards();

    void remove(QCard *card);
    void remove(CardList _cards);
    void remove(QList<CardType> _cards);

    void clear();

    QSize cardSize() const;
    void setSort(int _type = 0);

    void setPickCard(bool Ok);

    quint16 getUserId() const;
    void setUserId(const quint16 &userId);

    quint8 getRole() const;
    void setRole(const quint8 &role);

    bool isReady();
    void setReady(bool ready);

    quint8 getOrder() const;
    void setOrder(const quint8 &order);

    bool isHandle();
    void setHandle(bool bOk);

    void playCards(CardList _cards, bool self = false);
    void thinkPlayCars(CardList _cards, bool isLoard = false);

signals:
    void playCards(HandType _type, CardList _cards);

private:
    AreaType                m_areaType;
    QMap<CardType, QCard*>  m_cards;
    bool        m_pickCard;
    bool        m_isPressed;
    QPoint      m_startPos;
    QPixmap     m_pixmapBg;

    quint16     m_userId;
    quint8      m_order;
    quint8      m_role;
    bool        m_ready;
    bool        m_handle;
protected:
    void adjuest();
    void resizeEvent(QResizeEvent *e) override;
    void paintEvent(QPaintEvent *) override;
    void mousePressEvent(QMouseEvent *e) override;
    void mouseMoveEvent(QMouseEvent *e) override;
    void mouseReleaseEvent(QMouseEvent *e) override;
};

////////////////////////////////////////////////////////////////////
/// \brief The QCard class
class QCard {

public:
    QCard(CardType _type, const QPixmap & _pix);

    QRect rect() const;
    void setRect(const QRect &rect);

    bool selected() const;
    void setSelected(bool selected);

    QPixmap pixmap() const;
    void setPixmap(const QPixmap &pixmap);

    CardType type() const;

    bool pressed() const;
    void setPressed(bool pressed);

private:
    CardType m_type;
    QPixmap m_pixmap;
    QRect m_rect;
    bool m_selected;
    bool m_pressed;
};

#endif // QCARDAREA_H
